<?php

# Checking Whether a Variable Contains a Valid Number
# there are a variety of related functions with self-explanatory names: is_float() (or is_double() or is_real(); they’re all the same) and is_int() (or is_integer() or is_long().

foreach ([5, '5', "95", 12.3, 'five', 0xDECAFBAD, '10e200'] as $maybeNumber) {

    $isNumeric = is_numeric($maybeNumber);

    $actualType = gettype($maybeNumber);

    print "Is the $actualType $maybeNumber numeric? ";
    if (is_numeric($maybeNumber)) {
        print "yes";
    } else {
        print "no";
    }

    print "</br>";
}

echo "</br>";

# Comparing Floating-Point Numbers

$delta = 0.00001;

$a = 1.00000001;
$b = 1.00000000;

if (abs($a - $b ) < $delta) {
    print '$a and 5b are equal enough.';
} else {
    print "they're not equal enough" ;
}

echo "</br>";

# Roungding FLoating-point numbers

$number = round(2.4);
printf("2.4 rounds to the float %s", $number);
echo "</br>";

$number = ceil(2.4);
printf("2.4 rounds to the float %s", $number);
echo "</br>";

$number = floor(2.4);
printf("2.4 rounds to the float %s", $number);
echo "</br>";

# operating on a series of integers

echo "</br>";
$start = 3;
$end = 7;
for ($i = $start; $i <= $end; $i++) {
    printf ("%d squared is %d" . "<br/>", $i, $i * $i);
}

echo "</br>";
$start = 3;
$end = 7;
for ($i = $start; $i <= $end; $i += 2) {
    printf ("%d squared is %d" . "<br/>", $i, $i * $i);
}

echo "</br>";
$numbers = range(3,7);
foreach ($numbers as $n){
    printf ("%d squared is %d" . "<br/>", $n, $n * $n);
}

echo "</br>";
# 2.5 Generating Random Numbers Within a Range

$lower = 5;
$upper = 100;
$random_number = mt_rand($lower, $upper);
print $random_number;
echo "</br>";

# Predictable random number

/*function pick_color() {
    $colors = array("red", "orange", "yellow", "blue", "green", "indigo", "violet");
    $i = mt_rand(0, count($colors) - 1);
    return $colors[$i];
}

mt_srand(34534);
$first = pick_color();
$second = pick_color();
print "$first is red and $second is yellow"; */
echo "</br>";

# Formatting Numbers
echo "</br>";

$number = 1234.56;
$formatted = number_format($number, 2);

print $formatted;
